<?php
##############################################
#
#   用户管理
# @ Time : 2017-1-3 - 下午10:45:24
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Admin\Controller;
use Admin\Model\AdminBaseModel; 
use Admin\Model\UsersModel;


class UsersController extends AdminBaseController {
	
	protected $_users;
	
	public function _initialize(){
		parent::_initialize();
		$this->_users = new UsersModel();
	}
	
	/**
	 * 活动列表
	 */
	public function usersList(){
	    $data = $this->_users->getUsers(I('nickname'), I('phone_number'), I('email'), I('category'));
	    /*foreach ($data[0] as $k => $v){
	            $data[0][$k]['category'] = $this->_activity->getCategoryName($v['category']);
	    }
         */
	    $this -> assign('users', $data[0]);
	    $this -> assign('page', $data[1]);
	    $this -> display('index');
	}
	
	/**
	 * 添加会员
	 */
 	public function addUsers(){
		 if(IS_GET){
		    $id = I('id');
		    $act = I('get.act');
		    if($id && $act == 'edit'){
		        $data = $this->_users->getUsersFind($id);
		        $this->assign('id', $id);
		        $this->assign('users', $data);
		    }
		    $this->assign('act', $act);
		    $this->display('add');
		}else if(IS_POST){
		    $data = array(
		        'nickname' => I('nickname'),
		        'phone_number' => I('phone_number'),
		        'email' => I('email'),
		        'is_lock' => I('is_lock'),
		        'luckdraw_num' => I('luckdraw_num')
		    );
		   	$admin_base_model = new AdminBaseModel('users');
		   	$act = I('post.act');
		   	if($act == 'add'){
		   	    $result = $admin_base_model->addData($data);
		   	    if($result)
		   	        $this->success('添加成功', U('usersList'), 1);
		   	    else
		   	        $this->error('操作失败', '', 1);
		   	}elseif($act == 'edit'){
		   	    $id = I('id');
		   	    if($this->_users->editUsers($id, $data) !== false)
		   	        $this->success('修改成功', U('usersList'), 1);
		   	    else
		   	        $this->error('操作失败', '', 1);
		   	}
    	} 
	}

	/**
	 * 全选删除用户
	 */
    public function deleteAllUsers(){
	    if(!IS_POST) return;
	    $result = $this->_users->deleteAllUsers($_POST);
	    if($result)
			$this->success('删除成功', U('usersList'), 1);
		else
			$this->error('操作失败', '', 1);
	}
	
	/**
	 * 删除会员
	 */
	public function deleteUsers(){
		if(!IS_AJAX) return;
		$result = $this->_users->deleteUsers(intval($_GET['id']));
		if($result)
			$this->ajaxReturn(array('status' => 1,'message' => '删除成功'));
		else
			$this->ajaxReturn(array('status' => 2,'message' => '操作失败'));
	}
	
	
	/**
	 * 操作
	 */
	public function operate(){
	    $operate = I('post.operate');
	    $selected_id_arr = I('post.users_id');
	    if(!in_array($operate,array('del','show','hide')) || !is_array($selected_id_arr)){
	        $this->error('非法操作');
	    }
	    $map['id'] = array('in', $selected_id_arr);
	    $users = M('users');
	    switch ($operate){
	        case 'del':
	            $row = $users->where($map)->delete();
	            break;
	        case 'show':
	            $data = array('is_lock'=>1);
	            $row = $users->where($map)->save($data);
	            break;
	        case 'hide':
	            $data = array('is_lock'=>0);
	            $row = $users->where($map)->save($data);
	            break;
	    }
	    if(!$row)
	        $this->error('操作失败');
	    else
	        $this->success('操作成功', U('usersList'), 1);
	
	}
	
	//当前用户的活动列表
	public function usersActivityList(){
	    $user_id = I('id');
	    
	}

}



	
	